package com.axjy.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class LoginController {


    @GetMapping("/login")
    public String login(){
        return "login";
    }



    @PostMapping("/login")
    public String login(String userCode, String password, Model model){

        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(userCode,password);
        try {
           subject.login(token);
            return "redirect:/index";
        }catch (Exception ex){
            model.addAttribute("loginErr","用户名或密码错误");
            return "login";
        }

    }



}
